<template>
	<div class="sys-config-container">
		<el-dialog v-model="state.isShowDialog" draggable width="600px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<el-form :model="state.editForm" ref="editFormRef" label-width="120px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="供应商名称" prop="supplyName" :rules="[{ required: true, message: '供应商名称不能为空', trigger: 'blur' }]">
							<el-input v-model="state.editForm.supplyName" placeholder="供应商名称" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="中文名称" prop="supplyCNName" :rules="[{ required: true, message: '中文名称不能为空'}]">
							<el-input v-model="state.editForm.supplyCNName" placeholder="中文名称" clearable/>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="联系人" prop="contacts" :rules="[{ required: true, message: '联系人不能为空' }]">
							<el-input v-model="state.editForm.contacts" placeholder="联系人" clearable/>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="职务" prop="duty" :rules="[{ required: true, message: '职务不能为空' }]">
							<el-input v-model="state.editForm.duty" placeholder="职务" clearable/>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="电话" prop="tel" :rules="[{ required: true, message: '电话不能为空' }]">
							<el-input v-model="state.editForm.tel" placeholder="电话" clearable/>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="等级" prop="grade" :rules="[{ required: true, message: '等级不能为空' }]">
							<el-input v-model="state.editForm.grade" placeholder="等级" clearable/>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="地址" prop="address" :rules="[{ required: false, message: '地址不能为空' }]">
							<el-input v-model="state.editForm.address" placeholder="地址" clearable/>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="状态" prop="usable" :rules="[{ required: true, message: '状态不能为空', trigger: 'blur' }]">
							<el-radio-group v-model="state.editForm.usable">
								<el-radio :label="1">正常</el-radio>
								<el-radio :label="0">禁用</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="开户行" prop="bankName" :rules="[{ required: false, message: '开户行不能为空' }]">
							<el-input v-model="state.editForm.bankName" placeholder="开户行" clearable/>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="银行账号" prop="bankAccount" :rules="[{ required: false, message: '银行账号不能为空' }]">
							<el-input v-model="state.editForm.bankAccount" placeholder="银行账号" clearable/>
						</el-form-item>
					</el-col> 
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="EditForm">
import { reactive, ref } from 'vue';

import { getAPI } from '/@/utils/axios-utils';
import { SupplierApi } from '/@/api-services/api';
import { Supplier } from '/@/api-services/models';

const props = defineProps({
	title: String,
});
const emits = defineEmits(['updateData']);
const editFormRef = ref();
const state = reactive({
	isShowDialog: false,
	editForm: {} as Supplier,
});

// 打开弹窗
const openDialog = (row: any) => {
	console.log("========================");
	console.log(row);
	
	state.editForm = JSON.parse(JSON.stringify(row));
	state.isShowDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emits('updateData');
	state.isShowDialog = false;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 提交
const submit = () => {
	editFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
		if (state.editForm.id != undefined && state.editForm.id > 0) {
			await getAPI(SupplierApi).apiSupplierUpdatePost(state.editForm);
		} else {
			await getAPI(SupplierApi).apiSupplierAddPost(state.editForm);
		}
		closeDialog();
	});
};

// 导出对象
defineExpose({ openDialog });
</script>